import 'package:flutter/material.dart';


class HeartWidget extends StatefulWidget {
  const HeartWidget({Key key}) : super(key: key);

  @override
  State<HeartWidget> createState() => HeartState();
}

class ProvideData{
  bool isCheck = false;
  int num = 0;
}


class HeartState extends State<HeartWidget> {


  @override
  Widget build(BuildContext context) {

    return IconButton(
      icon: ShareDataWidget.of(context).data.isCheck
          ? const Icon(Icons.favorite, size: 50)
          : const Icon(Icons.favorite_border, size: 50),
      onPressed: () {
        setState(() {
          ShareDataWidget.of(context).data.isCheck = ShareDataWidget.of(context).data.isCheck ? false : true;

        });
      },
    );
  }
}

class ShareDataWidget extends InheritedWidget {
  ShareDataWidget({Key key,  this.data, Widget child})
      : super(key: key, child: child);

  /// 需要在组件树中共享的数据
  ProvideData data;

  static ShareDataWidget of(BuildContext context) {
    return context.dependOnInheritedWidgetOfExactType<ShareDataWidget>();
  }



  @override
  bool updateShouldNotify(covariant ShareDataWidget oldWidget) {
    // TODO: implement updateShouldNotify
    return oldWidget.data != data;
  }
}



class IGenericDataProvider<T> extends InheritedWidget {
  final T data;

  const IGenericDataProvider({Key key, Widget child, this.data})
      : super(key: key, child: child);

  @override
  bool updateShouldNotify(IGenericDataProvider oldWidget) {
    return data != oldWidget.data;
  }

  static IGenericDataProvider<T> of<T>(BuildContext context) {
    return context.dependOnInheritedWidgetOfExactType() as IGenericDataProvider<T>;
  }
}



class MyProvide with ChangeNotifier {

  MyProvide();

  /// ------ 阅读人数 -------
  int _num = 0;
  int get getNum => _num;
  void addNum() {
    _num++;
    notifyListeners();
  }

  /// -------- 收藏 ---------
  bool _isFavorite = false;
  bool get getIsFavorite => _isFavorite;
  void changeFavorite(bool isFavorite) {
    _isFavorite = isFavorite;
    notifyListeners();
  }
}